package logic

import (
	"context"
	"math/rand"
	"shopping-mall/models/redis"
	"time"

	"shopping-mall/shoppingrpc/internal/svc"
	"shopping-mall/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *shoppingrpc.SendSmsRequest) (*shoppingrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000

	//get := redis.Rdb.Get("sendSmsCount")
	//if get.Val() >= "1" {
	//	return nil, fmt.Errorf("短信限制1分钟只能发送一次")
	//}
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, fmt.Errorf("短信发送失败" + err.Error())
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	redis.Rdb.Set("sendSms"+in.Source+in.Mobile, code, time.Minute*5)
	//redis.Rdb.Set("sendSmsCount", code, time.Minute*1)
	return &shoppingrpc.SendSmsResponse{}, nil
}
